package org.fastiot.gateway.server

import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandler
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.MessageToByteEncoder
import org.apache.camel.component.netty4.DefaultChannelHandlerFactory
import org.fastiot.gateway.Message

class BytePacketEncoder extends DefaultChannelHandlerFactory {

    @Override
    ChannelHandler newChannelHandler() {
        return new MessageToByteEncoder<Message>() {
            @Override
            protected void encode(
                    final ChannelHandlerContext ctx, final Message msg, final ByteBuf out) throws Exception {
                out.writeBytes(msg.contents)
                ctx.flush()
            }
        }
    }
}
